﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Web.Script.Services;

namespace SES.CMS.Module
{
    /// <summary>
    /// Summary description for AddCartSV
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]

    public class AddCartSV : System.Web.Services.WebService
    {

        public AddCartSV()
        {

        }

        [WebMethod(EnableSession = true)]
        public bool AddSesson(int prdID, int qty)
        {
            if (qty == 0) qty = 1;
            cmsProductDO itemDO = new cmsProductDO();
            itemDO.ProductID = prdID;
            //itemDO.ProductID = idProduct;
            cmsProductDO product = new cmsProductBL().Select(itemDO);
            if (product != null)
            {
                ProductShop item = new ProductShop();
                item.Id = product.ProductID;
                item.Image = product.ProductImage;
                item.Name = product.ProductTitle;
                if (product.IsPromotion) //  Kiểm tra sản phẩm có khuyến mãi thì tính giá khuyến mãi(saleprice). ko thì tính giá bình thường(price)
                {
                    if (product.SalePrice == 0 || product.SalePrice.ToString().Equals(""))
                    {
                        item.Cost = 0;
                    }
                    else
                    {
                        item.Cost = (decimal)product.SalePrice;
                    }
                }
                // Sản phẩm không có giá khuyến mãi
                else
                {
                    if (product.Price == 0 || product.Price.ToString().Equals(""))
                        item.Cost = 0;
                    else
                    {

                        item.Cost = (decimal)product.Price;
                    }
                }
                item.Quantity = qty;
                ((MyShopCart)Session["shop"]).Add(item);
            }
            return true;
        }
        [WebMethod(EnableSession = true)]
        public string rtTotal()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shop"];
            return string.Format("{0:0,0}", cart.GetTotalCost());
        }
        [WebMethod(EnableSession = true)]
        public string rtNumber()
        {
            MyShopCart cart = new MyShopCart();
            cart = (MyShopCart)Session["shop"];
            return cart.GetTotalProduct().ToString();

        }
    }
}
